//
//  WmcIsolationTemplateView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/8.
//

import UIKit

/// 隔离天数水印模板
class WmcIsolationTemplateView: WmcBaseWatermarkTemplateView {
    lazy var topBgView: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        return view
    }()

    lazy var dayLabel: UILabel = {
        let label = UILabel.YP.Create(color: UIColor.white, alignment: .right)
        return label
    }()

    lazy var leftLinkImageView: UIImageView = {
        let imageView = UIImageView(image: "wm_link".wmc_image)
        return imageView
    }()

    lazy var rightLinkImageView: UIImageView = {
        let imageView = UIImageView(image: "wm_link".wmc_image)
        return imageView
    }()

    override func config() {
        super.config()
        defaultWidth = 230.wmc_autoHor

        addSubview(topBgView)

        addSubview(titleLabel)

        addSubview(dayLabel)

        backgroundColorView.backgroundColor = .black
        addSubview(backgroundColorView)

        addSubview(leftLinkImageView)

        addSubview(rightLinkImageView)

        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 0
        addSubview(stackView)
    }

    override func configWatermarkModel(_ watermarkModel: WmcWatermarkDetailModel, styleMode: WmcBaseWatermarkTemplateView.StyleMode) {
        super.configWatermarkModel(watermarkModel, styleMode: styleMode)

        let alpha = calculateAlpha(watermarkModel.alpha)
        topBgView.alpha = alpha
        leftLinkImageView.alpha = alpha
        rightLinkImageView.alpha = alpha
        backgroundColorView.alpha = alpha

        var drawViews: [UIView] = []
        // 获取标题最大字数
        let maxTitleCount = watermarkModel.fieldTitleMaxCount(filters: [.title, .isolation])
        watermarkModel.fields.forEach { option in
            guard option.isOpen else { return }
            switch option.fieldType {
            case .time: // 时间
                let item = createDateItem(option: option,
                                          styleMode: styleMode,
                                          staticDate: watermarkModel.staticData.staticDate,
                                          maxTitleCount: maxTitleCount,
                                          dateFormat: .yyyy_MM_dd_week)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .title: // 标题
                let text = option.contentText
                titleLabel.attributedText = text.yp.makeAttributed { make in
                    make.set(font: 36.yp.uiFont(weight: .bold), range: 0 ..< 1)
                        .set(font: 20.yp.uiFont(weight: .bold), range: 1 ..< text.count)
                        .set(foregroundColor:option.uiTextColor(.wmc_red_F54A45))
                }

            case .isolation: // 隔离天数
                let text = option.contentText("1")
                dayLabel.attributedText = "第\(text)天".yp.makeAttributed { make in
                    make.set(font: 16.yp.uiFont(weight: .bold), range: 0 ..< 1)
                        .set(font: 24.yp.uiFont(weight: .bold), range: 1 ..< 1 + text.count)
                        .set(font: 16.yp.uiFont(weight: .bold), range: 1 + text.count ..< text.count + 2)
                        .set(foregroundColor:option.uiTextColor)
                }

            case .addr: // 地点
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .location)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .lat: // 经纬
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .longitudLatitude)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .weather: // 天气
                let item = createWeatherItem(option: option,
                                             styleMode: styleMode,
                                             staticWeater: watermarkModel.staticData.staticWeater,
                                             maxTitleCount: maxTitleCount,
                                             weatherFormat: .weatherTemperature)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .altitude: // 海拔
                let item = createAltitudeItem(option: option,
                                              styleMode: styleMode,
                                              staticLocation: watermarkModel.staticData.staticLocation,
                                              maxTitleCount: maxTitleCount,
                                              altitudeFormat: .meters)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .azimuth: // 方位角
                let item = createAzimuthItem(option: option,
                                             styleMode: styleMode,
                                             staticAzimuth: watermarkModel.staticData.staticAzimuth,
                                             maxTitleCount: maxTitleCount,
                                             azimuthFormat: .default)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .decibe: // 分贝
                let item = createDecibeItem(option: option,
                                            styleMode: styleMode,
                                            maxTitleCount: maxTitleCount,
                                            decibeFormat: .describeDB)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .speed: // 速度
                let item = createSpeedItem(option: option,
                                           styleMode: styleMode,
                                           staticLocation: watermarkModel.staticData.staticLocation,
                                           maxTitleCount: maxTitleCount,
                                           speedFormat: .km_h)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .remark,
                 .record,
                 .text,
                 .custom: // 备注、记工、输入
                let item = createDefaultTextItem(option: option,
                                                 maxTitleCount: maxTitleCount)
                item.backgroundColor = option.uiBgColor.yp.alpha(alpha)
                drawViews.append(item)

            case .brand: // 品牌图
                configOptionBrand(option)

            default: break
            }
        }

        // 添加进堆栈视图
        stackView.yp.arrangedSubviews = drawViews
    }

    override func makeConstraints() {
        topBgView.snp.makeConstraints { make in
            make.top.left.equalTo(8)
            make.right.equalTo(-8)
            make.height.equalTo(40)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(16)
            make.bottom.equalTo(topBgView).offset(-8)
        }

        dayLabel.snp.makeConstraints { make in
            make.top.equalTo(topBgView)
            make.bottom.equalTo(topBgView).offset(-8)
            make.right.equalTo(-18)
        }

        leftLinkImageView.snp.makeConstraints { make in
            make.top.equalTo(topBgView.snp.bottom).offset(-8)
            make.left.equalTo(26)
            make.size.equalTo(CGSize(width: 6, height: 20))
        }

        rightLinkImageView.snp.makeConstraints { make in
            make.top.size.equalTo(leftLinkImageView)
            make.right.equalTo(-26)
        }

        // 布局
        super.makeConstraints()

        backgroundColorView.snp.makeConstraints { make in
            make.top.equalTo(topBgView.snp.bottom).offset(4)
            make.left.right.equalTo(topBgView)
            if isShowCertificationWatermark {
                make.bottom.equalTo(certificationView.snp.top).offset(-4)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }

        stackView.snp.makeConstraints { make in
            make.top.equalTo(backgroundColorView).offset(12)
            make.left.right.equalTo(backgroundColorView)
            make.bottom.equalTo(backgroundColorView).offset(-8)
        }
    }
}
